package com.example.bbfindhouse.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.bbfindhouse.R;

public class MaifangActivity extends AppCompatActivity {

    private Spinner spinnerCity;
    private EditText editTextCommunity, editTextAddress, editTextPrice, editTextContact, editTextPhone;
    private Button buttonSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maifang2);

        spinnerCity = findViewById(R.id.spinner_city);
        editTextCommunity = findViewById(R.id.editText_community);
        editTextAddress = findViewById(R.id.editText_address);
        editTextPrice = findViewById(R.id.editText_price);
        editTextContact = findViewById(R.id.editText_contact);
        editTextPhone = findViewById(R.id.editText_phone);
        buttonSubmit = findViewById(R.id.button_submit);

        // 设置城市下拉列表
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.cities, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCity.setAdapter(adapter);

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (validateInput()) {
                    Toast.makeText(MaifangActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private boolean validateInput() {
        String city = spinnerCity.getSelectedItem().toString();
        String phone = editTextPhone.getText().toString().trim();

        if (city.isEmpty() || phone.isEmpty()) {
            Toast.makeText(this, "城市和联系方式为必填项", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}